const btnSbt = document.querySelector("[type=submit]")
const form = document.querySelector("form")
const toast = new bootstrap.Toast("#outToast")
const toastBody = document.querySelector(".toast-body")
// 1.按钮绑定点击事件
btnSbt.addEventListener('click', async e => {
	e.preventDefault()
	// 	2.表单获取
	const data = serialize(form, {hash: true, empty: true})
	const {username, password} = data
	// 	3.检验数据
	if (username.length < 2 || username.length > 30) {
		toast.show()
		toastBody.innerHTML = "用户名不合规"
		return
	}
	if (password.length < 6 || password.length > 30) {
		toast.show()
		toastBody.innerHTML = "密码不合规"
		return
	}
	// 	4.发送数据
	try {
		const res = await axios({
			url: '/register',
			method: 'post',
			data,
		})
		toastBody.innerHTML = res.data.message
		// 6.跳转到登录页
		setTimeout(() => {
			location.href = "./logIn.html"
		},2000)
	} catch (e) {
		toastBody.innerHTML = e.response.data.message
	}
	// 	5.Toast 显示内容
	toast.show()
})
